{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.keras import datasets, layers, models"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()\n",
    "\n",
    "train_images, test_images = train_images / 255.0, test_images / 255.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = models.Sequential(\n",
    "[\n",
    "    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),\n",
    "    layers.MaxPooling2D((2, 2)),\n",
    "    layers.Conv2D(64, (3, 3), activation='relu'),\n",
    "    layers.MaxPooling2D((2, 2)), \n",
    "    layers.Conv2D(64, (3, 3), activation='relu'),\n",
    "    layers.Flatten(),\n",
    "    layers.Dense(64, activation='relu'),\n",
    "    layers.Dense(10)\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "conv2d (Conv2D)              (None, 30, 30, 32)        896       \n",
      "_________________________________________________________________\n",
      "max_pooling2d (MaxPooling2D) (None, 15, 15, 32)        0         \n",
      "_________________________________________________________________\n",
      "conv2d_1 (Conv2D)            (None, 13, 13, 64)        18496     \n",
      "_________________________________________________________________\n",
      "max_pooling2d_1 (MaxPooling2 (None, 6, 6, 64)          0         \n",
      "_________________________________________________________________\n",
      "conv2d_2 (Conv2D)            (None, 4, 4, 64)          36928     \n",
      "_________________________________________________________________\n",
      "flatten (Flatten)            (None, 1024)              0         \n",
      "_________________________________________________________________\n",
      "dense (Dense)                (None, 64)                65600     \n",
      "_________________________________________________________________\n",
      "dense_1 (Dense)              (None, 10)                650       \n",
      "=================================================================\n",
      "Total params: 122,570\n",
      "Trainable params: 122,570\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 50000 samples, validate on 10000 samples\n",
      "Epoch 1/10\n",
      "50000/50000 [==============================] - 29s 582us/sample - loss: 1.5188 - accuracy: 0.4465 - val_loss: 1.2987 - val_accuracy: 0.5322\n",
      "Epoch 2/10\n",
      "50000/50000 [==============================] - 29s 589us/sample - loss: 1.1381 - accuracy: 0.5957 - val_loss: 1.0451 - val_accuracy: 0.6243\n",
      "Epoch 3/10\n",
      "50000/50000 [==============================] - 30s 600us/sample - loss: 0.9860 - accuracy: 0.6516 - val_loss: 0.9802 - val_accuracy: 0.6520\n",
      "Epoch 4/10\n",
      "50000/50000 [==============================] - 33s 656us/sample - loss: 0.8883 - accuracy: 0.6865 - val_loss: 0.9151 - val_accuracy: 0.6756\n",
      "Epoch 5/10\n",
      "50000/50000 [==============================] - 33s 658us/sample - loss: 0.8196 - accuracy: 0.7122 - val_loss: 0.8595 - val_accuracy: 0.7034\n",
      "Epoch 6/10\n",
      "50000/50000 [==============================] - 34s 685us/sample - loss: 0.7584 - accuracy: 0.7314 - val_loss: 0.9211 - val_accuracy: 0.6820\n",
      "Epoch 7/10\n",
      "50000/50000 [==============================] - 34s 684us/sample - loss: 0.7085 - accuracy: 0.7499 - val_loss: 0.8999 - val_accuracy: 0.6924\n",
      "Epoch 8/10\n",
      "50000/50000 [==============================] - 34s 687us/sample - loss: 0.6648 - accuracy: 0.7660 - val_loss: 0.9227 - val_accuracy: 0.6894\n",
      "Epoch 9/10\n",
      "50000/50000 [==============================] - 35s 706us/sample - loss: 0.6225 - accuracy: 0.7816 - val_loss: 0.8758 - val_accuracy: 0.7119\n",
      "Epoch 10/10\n",
      "50000/50000 [==============================] - 34s 683us/sample - loss: 0.5805 - accuracy: 0.7943 - val_loss: 0.8774 - val_accuracy: 0.7097\n"
     ]
    }
   ],
   "source": [
    "model.compile(optimizer='adam',\n",
    "              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n",
    "              metrics=['accuracy'])\n",
    "\n",
    "history = model.fit(train_images, train_labels, epochs=10, \n",
    "                    validation_data=(test_images, test_labels))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10000/1 - 3s - loss: 0.8607 - accuracy: 0.7097\n",
      "0.7097\n"
     ]
    }
   ],
   "source": [
    "test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)\n",
    "print(test_acc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.save('keras-cifar-10/weights', save_format='h5')\n",
    "\n",
    "model_json = model.to_json()\n",
    "with open(\"keras-cifar-10/model.json\", \"w\") as json_file:\n",
    "    json_file.write(model_json)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
